/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ies.domain.model.web.util;

import com.ies.domain.model.repository.util.AbstractFacade;
import com.ies.domain.model.util.JpaPool;
import com.ies.domain.model.util.ModelInterface;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author gustavo
 */
public abstract class AbstractCrudManagedBean<T> extends AbstractManagedBean {

    protected T entity;
    protected T[] entities;
    protected LazyDataModel<T> lazyDataModel;

    public AbstractCrudManagedBean() {
        init();
    }

    public AbstractCrudManagedBean(T entity) {
        this.entity = entity;
    }

    public void newEntity() {
        init();
    }

    public String save() {
        if (((ModelInterface) entity).getId() == null || ((ModelInterface) entity).getId() == 0) {
            JpaPool.getInstancia().getFacade(entity.getClass()).create(entity);
            ViewMessage.AddInfo(entity.getClass().getSimpleName() + " foi cadastrado com sucesso!");
        } else {
            JpaPool.getInstancia().getFacade(entity.getClass()).edit(entity);
            ViewMessage.AddInfo(entity.getClass().getSimpleName() + " foi editado com sucesso!");
        }

        init();
        return "index";
    }

    public void delete() {
        entity = (T) JpaPool.getInstancia().getFacade(entity.getClass()).find(((ModelInterface) entity).getId());
        if (entity != null) {
            JpaPool.getInstancia().getFacade(entity.getClass()).remove(entity);
            ViewMessage.AddInfo(entity.getClass().getSimpleName() + " foi removido com sucesso!");
        }
    }

    public T getEntity() {
        if (entity == null) {
            init();
        }
        if (getRequestParam("id") != null && !getRequestParam("id").isEmpty()) {
            if (((ModelInterface) entity).getId() == null || ((ModelInterface) entity).getId() != Long.parseLong(getRequestParam("id"))) {
                entity = (T) JpaPool.getInstancia().getFacade(entity.getClass()).find((Long.parseLong(getRequestParam("id"))));
            }
        }
        return entity;
    }

    public LazyDataModel<T> getLazyDataModel() {
        if (lazyDataModel == null) {
            lazyDataModel = new LazyDataModel<T>() {
                @Override
                public Object getRowKey(T object) {
                    return (((ModelInterface) object).getId());
                }

                @Override
                public List<T> load(int offset, int max, String order, SortOrder so, Map<String, String> map) {
                    int cont = JpaPool.getInstancia().getFacade(entity.getClass()).count().intValue();
                    this.setRowCount(cont == 0 ? -1 : cont);
                    List<T> lista = JpaPool.getInstancia().getFacade(entity.getClass()).findAll();
                    return lista;
                }
            };
            if (entity == null) {
                init();
            }
            int cont = JpaPool.getInstancia().getFacade(entity.getClass()).count().intValue();
            lazyDataModel.setRowCount(cont == 0 ? -1 : cont);
        }

        return lazyDataModel;
    }

    public void setLazyDataModel(LazyDataModel<T> lazyDataModel) {
        this.lazyDataModel = lazyDataModel;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }

    public abstract void init();

    public T[] getEntities() {
        return entities;
    }

    public void setEntities(T[] entities) {
        this.entities = entities;
    }
}
